package com.synergy.stockflow.procure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.procure.entity.AcceptanceOnArrival;
import com.synergy.stockflow.procure.entity.PurchaseReturns;
import com.synergy.stockflow.procure.entity.ReturnDetails;
import com.synergy.stockflow.procure.service.*;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.service.SysUserService;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/purchasseReturns")
public class PurchasseReturnsController {
    @Autowired
    PurchaseReturnsService purchaseReturnsService;
    @Autowired
    ReturnDetailsService returnDetailsService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    InventoryTransactionService inventoryTransactionService;
    @Autowired
    AcceptanceOnArrivalService acceptanceOnArrivalService;
    @Autowired
    PurchaseDetailsService purchaseDetailsService;

    @GetMapping
    public Result<Page<PurchaseReturns>> getPurchaseReturns(
            @RequestParam(name = "purchaseReturnsId", required = false) String purchaseReturnsId,
            @RequestParam(name = "purchaseReturnsPurchaseOrderId", required = false) String purchaseReturnsPurchaseOrderId,
            @RequestParam(name = "purchaseReturnsReturnsWarehouse", required = false) String purchaseReturnsReturnsWarehouse,
            @RequestParam(name = "purchaseReturnsConfirm", required = false) String purchaseReturnsConfirm,
            @RequestParam(name = "purchaseReturnsReturnTime", required = false) String purchaseReturnsReturnTime,
            @RequestParam(name = "purchaseReturnsAttn", required = false) String purchaseReturnsAttn
    ){
        Page<PurchaseReturns> page = new Page<>(1, 10000);
        page.setRecords(purchaseReturnsService.selectAll(page,purchaseReturnsId, purchaseReturnsPurchaseOrderId, purchaseReturnsReturnsWarehouse, purchaseReturnsConfirm, purchaseReturnsReturnTime, purchaseReturnsAttn));
        for(PurchaseReturns p:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            p.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,p.getPurchaseReturnsAttn())).getNickname());
        }
        return Result.success(page);
    }

    @PostMapping
    public int insert(@RequestBody PurchaseReturns purchaseReturns){
        purchaseReturnsService.save(purchaseReturns);
        List<PurchaseReturns> list = purchaseReturnsService.list();
        for(ReturnDetails r:purchaseReturns.getReturnDetailsList()){
            r.setReturnDetailsPurchaseReturnsId(list.get(list.size()-1).getPurchaseReturnsId());
            r.setReturnDetailsId("RD-"+String.valueOf(new Date().getTime()));
            returnDetailsService.save(r);
            if(purchaseReturns.getPurchaseReturnsConfirm().equals("已确认")){
                String warehouseId =  purchaseReturns.getPurchaseReturnsReturnsWarehouse();
                String sourceDocId=list.get(list.size()-1).getPurchaseReturnsId();
                double v=r.getReturnDetailsNumberOfReturns().doubleValue()-r.getReturnDetailsNumberOfReturns().doubleValue()*2;
                String materialId=purchaseDetailsService.selectWa(r.getReturnDetailsInboundDetailsId()).getPurchaseDetailsProductsId();
                InventoryTransaction inventoryTransaction = new InventoryTransaction(null,"PO-IN",sourceDocId,"purchasse_returns",warehouseId,materialId,r.getReturnDetailsId(),new BigDecimal(v),new Date(),"1","验收退货",null,null);
                inventoryTransactionService.save(inventoryTransaction);
            }
        }

        return 1;
    }

    @PutMapping
    public int update(@RequestBody PurchaseReturns purchaseReturns){
        purchaseReturnsService.updateById(purchaseReturns);
        if(purchaseReturns.getPurchaseReturnsConfirm().equals("已确认")){
            for(ReturnDetails r:purchaseReturns.getReturnDetailsList()){
                    String warehouseId =  purchaseReturns.getPurchaseReturnsReturnsWarehouse();
                    String sourceDocId=purchaseReturns.getPurchaseReturnsId();
                    double v=r.getReturnDetailsNumberOfReturns().doubleValue()-r.getReturnDetailsNumberOfReturns().doubleValue()*2;
                    String materialId=purchaseDetailsService.selectWa(r.getReturnDetailsInboundDetailsId()).getPurchaseDetailsProductsId();
                    InventoryTransaction inventoryTransaction = new InventoryTransaction(null,"PO-IN",sourceDocId,"purchasse_returns",warehouseId,materialId,r.getReturnDetailsId(),new BigDecimal(v),new Date(),"1","验收退货",null,null);
                    inventoryTransactionService.save(inventoryTransaction);
            }
        }
        return 1;
    }

    @DeleteMapping
    public int delete(@RequestBody List<PurchaseReturns> purchaseReturns){
        for (PurchaseReturns p:purchaseReturns){
            purchaseReturnsService.removeById(p.getPurchaseReturnsId());
            returnDetailsService.deleteAll(p.getPurchaseReturnsId());
        }
        return 1;
    }

}
